package org.example;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.baomidou.mybatisplus.generator.model.ClassAnnotationAttributes;

/**
 * Hello world!
 */
public class Gen {

    static String url="jdbc:mysql://localhost:3306/local_test?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8";
    static String username="root";
    static String pwd ="root1234";



    public static void main(String[] args) {

//        String tableName= getTables(getConsolText()).toString();
        String tableName= "product";

        //父包名
        String parentPackage = "com.product";



        List<ClassAnnotationAttributes> attributesList = List.of(new ClassAnnotationAttributes("@Data","lombok.Data"), new ClassAnnotationAttributes("@Builder","lombok.Builder")
                , new ClassAnnotationAttributes("@NoArgsConstructor","lombok.NoArgsConstructor"), new ClassAnnotationAttributes("@AllArgsConstructor","lombok.AllArgsConstructor"));
//        ClassAnnotationAttributes[] dataAnnot = new ClassAnnotationAttributes[attributesList.size()];

        // 实体 @data @Builder @NoArgsConstructor @AllArgsConstructor
//        attributesList.toArray(dataAnnot);
        // 单一实体@data    二选一
        ClassAnnotationAttributes[] dataAnnot= new ClassAnnotationAttributes[]{new ClassAnnotationAttributes("@Data", "lombok.Data")};

        // 使用 FastAutoGenerator 快速配置代码生成器

        FastAutoGenerator.create(url, username, pwd)
                .globalConfig(builder -> {
                    builder.author("zhoushunquan") // 设置作者
                            .enableSwagger()
                            .outputDir("gen"); // 输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(parentPackage) // 设置父包名
                            .entity("entity") // 设置实体类包名
                            .mapper("mapper") // 设置 Mapper 接口包名
                            .service("service") // 设置 Service 接口包名
                            .serviceImpl("service.impl") // 设置 Service 实现类包名
                            .controller("controller")
                            .xml("mapper"); // 设置 Mapper XML 文件包名
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tableName)
//                    builder.addInclude("table1", "table2") // 设置需要生成的表名
                            .entityBuilder()
                            .superClass("Model")
                            .idType(IdType.INPUT)
                            .enableLombok(dataAnnot) // 启用 Lombok
                            .addTableFills(new Property("createTime", FieldFill.INSERT))
                            .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
                            .enableTableFieldAnnotation() // 启用字段注解
                            .enableFileOverride()

                            .controllerBuilder()
                            .enableRestStyle() // 启用 REST 风格
                            .enableHyphenStyle()//驼峰命名
                            .enableFileOverride()

                            .serviceBuilder().enableFileOverride()
                            .mapperBuilder().superClass("HoneycomBaseMapper").enableFileOverride();
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用 Freemarker 模板引擎
                .execute(); // 执行生成
        System.out.println("注意：每次生成都会覆盖上次生成的文件" +
                "\n实体类继承model 写法不完整");
    }

    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

    public static String getConsolText()  {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入表名，多个英文逗号分隔，所有输入 all：");
        if (scanner.hasNext()) {
            String inputStr = scanner.next();
            if (null!=inputStr&&!inputStr.isEmpty()) {
                return inputStr;
            }
        }
        return "";
    }

}
